<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('物流拼票')" />
	<th:block th:include="include :: list-css" />
	<style type="text/css">
		.layui-tab-title{border-color: #ccc !important;}
		.layui-this{color: rgb(0, 135, 250) !important;background: none !important;}
		.layui-tab-brief > .layui-tab-title .layui-this:after, .layui-tab-brief > .layui-tab-more li.layui-this:after{border-color:  rgb(0, 135, 250) !important;}
		label.layui-form-label {width: 95px;padding-right: 0px;}
		.layui-form-radio {margin-top:0}
		.mid-content{overflow-y:auto}
		.importTable{empty-cells:show;border-collapse:collapse;border-spacing:0;}
		.listext th{background:#eee;color:#3366cc;}
		.listext th,.listext td{border:solid 1px #ddd;text-align:left;padding:10px;font-size:14px;}
		#importProgress {z-index:20200202;background-color: #000;opacity: 0.3;display:none;}
		.importProgress {width:300px;z-index:20202222;position: fixed;top: 34%;left: 40%;display:none;}
	</style>
</head>
<body>
<div class="main-content">
	<div class="content-right">
		<div class="search-top">
			<span>查询条件</span>
			<a href="javascript:;" layadmin-event="flexible" class="menuBtn">
				<i class="layui-icon layui-icon-spread-left" id="LAY_app_flexible" style="color: #fff;"></i>
			</a>
		</div>
		<div class="panel layout-expand layui-expand-east">
			<div class="panel-header">
				<div class="panel-tool" style="text-align: center; width: 26px;height: 30px;line-height: 30px;">
					<a href="javascript:;" layadmin-event="flexible" class="menuBtn">
						<i class="layui-icon layui-icon-shrink-right" id="LAY_app_flexible" style="color: #fff;"></i>
					</a>
				</div>
			</div>
			<div class="panel-body panel-title" style="text-align: center; width: 26px;">查<br>询<br>条<br>件</div>
		</div>
		<form class="layui-form">
			<th:block th:include="listinclude :: searchRight('page')" />
		</form>
	</div>
	<div class="mid-content">
		<table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
		<div class="spxx" style="display: none;">
			<div class="layui-tab layui-tab-brief demo2" lay-filter="demo2" style="position:relative;">
				<ul class="layui-tab-title">
					<li class="detailsName">海外头程装箱清单</li>
				</ul>
				<div class="layui-tab-content"></div>
			</div>
			<table class="layui-hide" id="pdhtable" lay-filter="pftable"></table>
		</div>
	</div>
</div>
<!-- 表格头部工作栏-->
<script type="text/html" id="toolbar">
	<div class="layui-btn-container">
		<div class="layui-btn layui-table-tools importbtn-bg" lay-event="addbtn">
			<i class="dhicon dh-plus"></i> 拼票
		</div>
		<div class="layui-btn layui-table-tools importbtn-bg htbtn" lay-event="htbtn">
			<i class="dhicon layui-icon-prev"></i> 回退
		</div>
		<div class="layui-btn layui-table-tools importbtn-bg ticketPooling" lay-event="ticketPooling">
			<i class="dhicon layui-icon-prev"></i> 拼票预测
		</div>
		<div class="layui-btn layui-table-tools importbtn-bg" lay-event="pfbtn">
			<i class="dhicon dh-plus"></i> 抛重发货
		</div>
	</div>
</script>

<th:block th:include="include :: footer" />
<th:block th:include="include :: businessJS" />
<th:block th:include="include :: searchJS" />
<th:block th:include="listinclude :: list-templeat" />
<script th:src="@{/dhecp/js/queryCondition.js}"></script>
<script th:inline="javascript">
	/*<![CDATA[*/
	var prefix = ctx + "logstics/fbaSpellTicket";
	var gridColumns = [[${gridColumns}]],
			importFields =[[${importFields}]],
			moduleTable =[[${moduleTable}]];
	/*]]>*/
</script>
<script type="text/javascript">
	var element,thStr;
	var fieldWidth = {},		//导入列表字段宽度
			filedRequired = {},		//导入列表必填字段
			dateFiled = [],			//导入列表日期字段
			hasNull = false;		//导入数据必填字段有空值
	$.each(gridColumns.dictionaryForPageFieldList,function(i,v){
		fieldWidth[v.fields] = v.fieldFrontWidth
		if(v.fieldFrontRequired)filedRequired[v.fields] = v.fieldName;
		if(v.fieldFrontType == 't_date')dateFiled.push(v.fields);
	})
	var $,layer,laydate,form,table,wherestr={};
	layui.use(['table','laydate','form','layer','jquery','element'], function () {
		$ = layui.$, layer = layui.layer,laydate = layui.laydate , form = layui.form, table = layui.table, element = layui.element;
		serachRender();//查询条件初始渲染集合
		changeDropdown('fSl');//导入窗口——税率下拉可编辑赋事件
		$.tablelist.init({
			elem: '#dhtable'
			,modalName: "物流拼票"
			,url: prefix + '/list'
			,updateUrl: prefix + "/edit?guid={id}"
			,initSort: {field: 'createTime',type: 'asc'}
			,totalRow: gridColumns.totalRow
			,cols: [gridColumns.gridBody]
			,toolbar: '#toolbar'
			,defaultToolbar: [{title:'列设置',layEvent: 'fieldSet',icon: 'layui-icon-set'}] //这里在右边显示
			,parseData: function(res){ //res 即为原始返回的数据
				var objpage=sessionStorage.getItem(moduleTable+'checkdata')
				if(objpage){
					objpage=JSON.parse(objpage)
					res.data.map((v,i)=>{
						objpage.map((n,m)=>{
							if(v.guid==n.guid){
								v.LAY_CHECKED=true
							}
						})
					})
				}
			}
			,done:function(res, curr, count){
				var ltv=this.elem.next('.layui-table-view');//当前table的dom
				res.data.length || count || ltv.find('.layui-table-header').css('display', 'inline-block');//数量为0时(统计报表存在不传count的情况)，表头赋予样式可拉动
				res.data.length || count || ltv.find('.layui-table-box').css('overflow-x', 'auto');//数量为0时(统计报表存在不传count的情况)，表头赋予样式可拉动
				if($('.spxx').is(':hidden')){
					$('.layui-table-body').eq(0).css('height', "75vh");
				}else{
					$('.layui-table-body').eq(0).css('height', "40vh");
				}
				fixTotal(this);//修复合计行固定列问题
				rowheight(res, curr);//修复合计行固定列问题
				if(typeof listTableDone == 'function')listTableDone();//如果查询列表存在表格加载完成回调事件，则执行
			}
		});
		//监听排序事件
		table.on('sort(ftable)', function(obj){
			$.tablelist.sort('dhtable',obj);
		});

		// 多选赋值
		sessionStorage.setItem(moduleTable+'checkdata','')
		table.on('checkbox(ftable)', function(obj){
			var objpage=sessionStorage.getItem(moduleTable+'checkdata')
			if(obj.type=='all'){//判断是否全选
				var objdata=table.checkStatus('dhtable')
				objdata=objdata.data
				if(obj.checked){//判断是否选中
					if(objpage){
						objpage=JSON.parse(objpage)
						objpage=objpage.concat(objdata)
						objpage=newData(objpage)
					}else{
						objpage=objdata
					}
				}else{
					var objpage=JSON.parse(objpage)
					var deldata=table.cache.dhtable
					objpage=delData(objpage,deldata)
				}
				sessionStorage.setItem(moduleTable+'checkdata',JSON.stringify(objpage))
			}else{//点击复选框
				if(objpage){
					objpage=JSON.parse(objpage)
					if(obj.checked){
						objpage.push(obj.data)
					}else{
						objpage.map((v,i)=>{
							if(v.guid==obj.data.guid){
								objpage.splice(i,1)
							}
						})
					}
				}else{
					objpage=[]
					objpage.push(obj.data)
				}
				sessionStorage.setItem(moduleTable+'checkdata',JSON.stringify(objpage))
			}
		});

		//头工具栏事件
		table.on('toolbar(ftable)', function(obj){
			switch(obj.event){
				case 'addbtn':
					var objpage=sessionStorage.getItem(moduleTable+'checkdata')
					if(!objpage || objpage == '[]'){
						layer.msg("请先选择数据！");
					}else{
						$.modal.confirmCom('确定要拼票么？',function(result) {
							var infoItems = JSON.parse(objpage);
							let voluntarily = infoItems.some(item => item.stateShareTicket == 1);
							let handMovement = infoItems.some(item => item.stateShareTicket == 0);
							if (voluntarily && handMovement) {
								layer.msg("选择单据拼票类型只允许单一选择,请核实后在操作!");
								return;
							}
							let destinat = new Set();
							let wlfs = new Set();
							let guidList = ''
							infoItems.map((v,i)=> {
								destinat.add(v.destinationFulfillmentCenterId);
								wlfs.add(v.nameLogsticsWay);
								guidList+= v.guid + ','
							})
							guidList= guidList.substring(0,guidList.length - 1)

							if (destinat.size != 1){
								layer.msg("选择单据仓库标识只允许单一选择,请核实后在操作!");
								return;
							}
							if (wlfs.size != 1){
								layer.msg("选择单据物流方式只允许单一选择,请核实后在操作!");
								return;
							}
							layer.closeAll('dialog');
							//openTabsPage跳转
							if (voluntarily){
								top.layui.index.openTabsPage(prefix+ "/add?guidList="+guidList+'&type=1','物流拼票')
							} else {
								top.layui.index.openTabsPage(prefix+ "/add?guidList="+guidList+'&type=0','物流不拼票')
							}
						});
					}
					break;
				case 'fieldSet':
					filedSet(moduleTable);
					break;
				case 'ticketPooling'://拼票预测
					var objpage=sessionStorage.getItem(moduleTable+'checkdata')
					if(!objpage || objpage == '[]'){
						layer.msg("请先选择数据！");
					}else{
						$.modal.confirmCom('确定要拼票预测么？',function(result) {
							var infoItems = JSON.parse(objpage);
							let voluntarily = infoItems.some(item => item.stateShareTicket == 1);
							let handMovement = infoItems.some(item => item.stateShareTicket == 0);
							if (voluntarily && handMovement) {
								layer.msg("选择单据拼票类型只允许单一选择,请核实后在操作!");
								return;
							}
							let destinat = new Set();
							let guidList = ''
							let wlfs = new Set();
							infoItems.map((v,i)=> {
								destinat.add(v.destinationFulfillmentCenterId);
								wlfs.add(v.nameLogsticsWay);
								guidList+= v.guid + ','
							})
							guidList= guidList.substring(0,guidList.length - 1)

							if (destinat.size != 1){
								layer.msg("选择单据仓库标识只允许单一选择,请核实后在操作!");
								return;
							}
							if (wlfs.size != 1){
								layer.msg("选择单据物流方式只允许单一选择,请核实后在操作!");
								return;
							}
							layer.closeAll('dialog');
							let width = ($(window).width() - 500) + 'px';
							let height = ($(window).height() - 150) + 'px';
							$.ajax({
								url: prefix + '/ticketPooling',
								type: 'post',
								data: {guidList:guidList},
								dataType: 'html',
								success: function (result) {
									layer.open({
										type: 1,
										area: [width, height],
										fix: false,//不固定
										offset: '10%',//距离顶部
										maxmin: true,//最大最小化
										shade: 0.3,//遮罩
										title: '拼票预测',
										content:result,
										btn: ['确定', '取消'],
										shadeClose: true,// 弹层外区域关闭
									});
								}
							});
						});
					}
					break;
				case 'htbtn'://回退
					var objpage=sessionStorage.getItem(moduleTable+'checkdata')
					if(!objpage || objpage == '[]'){
						layer.msg("请先选择数据！");
					}else {
						$.modal.confirmCom('确定要回退么？', function (result) {
							var infoItems = JSON.parse(objpage)
							urlSh = prefix+'/htbtn';
							showloadings(true);
							$.operate.post(urlSh, {
								infoItems: JSON.stringify(infoItems)
							}, function (result) {
								if (result.code == 0) {
									setTimeout(function () {
										sessionStorage.setItem(moduleTable + 'checkdata', '')
										showloadings(false);
										table.reload('dhtable', {page: {curr: 1}, where: getWherestr()});
									}, 1000);
								} else {
									showloadings(false);
								}
							});
						});
					}
					break;
				case 'pfbtn':
					var objpage=sessionStorage.getItem(moduleTable+'checkdata')
					if(!objpage || objpage == '[]'){
						layer.msg("请先选择数据！");
					}else{
						$.modal.confirmCom('确定要抛重发货么？',function(result) {
							var infoItems = JSON.parse(objpage);
							let isLoadDelivery = infoItems.some(item => item.loadDelivery != '是');
							if (isLoadDelivery) {
								layer.msg("选择单据只允许是抛重发货,请核实后在操作!");
								return;
							}
							let voluntarily = infoItems.some(item => item.loadDelivery != '是');
							let handMovement = infoItems.some(item => item.stateShareTicket == 0);
							if (voluntarily && handMovement) {
								layer.msg("选择单据拼票类型只允许单一选择,请核实后在操作!");
								return;
							}
							let destinat = new Set();
							let wlfs = new Set();
							let guidList = ''
							infoItems.map((v,i)=> {
								destinat.add(v.destinationFulfillmentCenterId);
								wlfs.add(v.nameLogsticsWay);
								guidList+= v.guid + ','
							})
							guidList= guidList.substring(0,guidList.length - 1)

							if (destinat.size != 1){
								layer.msg("选择单据仓库标识只允许单一选择,请核实后在操作!");
								return;
							}
							if (wlfs.size != 1){
								layer.msg("选择单据物流方式只允许单一选择,请核实后在操作!");
								return;
							}
							layer.closeAll('dialog');
							//openTabsPage跳转
							top.layui.index.openTabsPage(prefix+ "/add?guidList="+guidList+'&type=2','物流抛重发货')
						});
					}
					break;
			};
		});


		//单击选中行
		table.on('row(ftable)', function(obj){
			var code = obj.data.code;
			let num = 3;
			$('.demo2 .layui-tab-title li').removeClass('layui-this')
			$('.layui-tab-title li').attr('style','background:#fff;color:#666666');
			$('.demo2 .layui-tab-title li').eq(0).addClass('layui-this')
			$.get(prefix+'/sonlist',{code:code,num:num,creationMethod:obj.data.creationMethod,codeShipment:obj.data.codeShipment},function(res){
				var datalist=res.data.sonList;
				var cols=[res.data.gridColumns.gridBody];
				var totalRow=res.data.gridColumns.totalRow;
				if (num == 2 ||num == 3 ||num == 4 ||num == 5){
					if (obj.data.creationMethod == '手动'){
						cols = tablecolsFh();
					} else {
						cols = tablecolsZD();
						//根据coleSize编辑出多少个箱子
						for (let i = 0; i < obj.data.numBox; i++) {
							let  tableData = {
								"align": "center",
								"field": "numRealityDeliver"+(i+1),
								"title": "箱子"+(i+1),
								"width": 95,
							}
							cols[0].push(tableData);
						}

						//将箱子数据赋值
						if (obj.data.numBox != 0){
							for (let i = 0; i < datalist.length; i++) {
								let ss = datalist[i].boxList.slice(1,datalist[i].boxList.length-1)
								let aa =  ss.split(",");
								for (let j = 0; j < aa.length; j++) {
									let numRe = 'numRealityDeliver'+(j+1)
									datalist[i][numRe] = aa[j]==0?"":aa[j]
								}
							}
						}
					}
				}
				$.tablelist.init({
					elem: '#pdhtable'
					,data:datalist
					,limit: 99999
					,where: getWherestr()
					,totalRow:totalRow
					,cols: cols
					,defaultToolbar: [{title:'列设置',layEvent: 'fieldSet',icon: 'layui-icon-set'}]
					,page:false
					// ,height:moduleTable==''?'full-46':''
					,parseData: function(res){ //res 即为原始返回的数据
						return {
							"code": res.code, //解析接口状态
							"msg": res.msg, //解析提示文本
							"count": res.count, //解析数据长度
							"data": decimalToFixed(res.data,gridColumns.decimalObj) //表格数据数值类型重赋值
						}
					}
					,done:function(res, curr, count){
						var ltv=this.elem.next('.layui-table-view');//当前table的dom
						res.data.length || count || ltv.find('.layui-table-header').css('display', 'inline-block');//数量为0时(统计报表存在不传count的情况)，表头赋予样式可拉动
						res.data.length || count || ltv.find('.layui-table-box').css('overflow-x', 'auto');//数量为0时(统计报表存在不传count的情况)，表头赋予样式可拉动
						res.data.length || count || ltv.find('.layui-table-body').css('height', ltv.find('.layui-table-body').height()-17);//数量为0时，表格高度减少滚动条高度
						$('.spxx').show()
						$('.layui-table-body.layui-table-main').eq(0).css('height', "35vh");
						$('.layui-table-body.layui-table-main').eq(1).css('height', "30vh");
						$('.layui-table-body').eq(1).css('height', "calc(35vh - 15px)");
						fixTotal(this);//修复合计行固定列问题
						if(typeof listTableDone == 'function')listTableDone();//如果查询列表存在表格加载完成回调事件，则执行
					}
					,jump:function(obj, first){
					}
				})
			})
		});
	});


	// 筛选1个数组相同的数据
	function newData(arr) {
		const map = {};
		arr.forEach(item => {
			const obj = {};
			Object.keys(item).sort().map(key => obj[key] = item[key]);
			map[JSON.stringify(obj)] = item;
		});
		return Object.keys(map).map(key => JSON.parse(key));
	}

	// 筛选2个数组相同的数据
	function delData(data, deldata) {
		data.forEach(item => {
			deldata = deldata.filter(items => items.guid != item.guid);
		});
		return deldata;
	}

</script>
</body>
</html>